﻿using BCDto.UploadHandleComponent;
using BCDto.WeChatComponent.WeChatNewsImageRecord;
using BCService.WeChatComponent.WeChatNewsImageRecordService;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using WeChatSDK.WeChatDto.Material;
using WeChatSDK.WeChatDto.Request.Http;
using WeChatSDK.WeChatServiceComponent.Material;

namespace BCService.WeChatComponent.WeChatMaterialService.WeChatNewsMaterialService
{
    /// <summary>
    /// 微信图文素材服务
    /// </summary>
    public class WeChatNewsMaterialService : IWeChatNewsMaterialService
    {
        private readonly IWeChatNewsImageRecordService weChatNewsImageRecordService;
        private readonly NewsMaterialService newsMaterialService;
        private readonly MaterialService materialService;
        
        public WeChatNewsMaterialService(IWeChatNewsImageRecordService weChatNewsImageRecordService)
        {
            this.newsMaterialService = new NewsMaterialService();
            this.materialService = new MaterialService();
            this.weChatNewsImageRecordService = weChatNewsImageRecordService;
        }

        public string AddPermanentNews(NewsMaterialDto newsMaterialDto)
        {
            return WeChatHttpResponseHandle.ProcessMessage(this.newsMaterialService.AddPermanentNews(newsMaterialDto));
        }

        public void DeletePermanentNews(string mediaId)
        {
            WeChatHttpResponseHandle.ProcessMessage(this.materialService.DeletePermanentMaterial(mediaId));
        }

        public void UpdatePermanentNewsMaterial(string mediaId, int index, NewsArticles newsArticles)
        {
            WeChatHttpResponseHandle.ProcessMessage(this.newsMaterialService.UpdatePermanentNewsMaterial(mediaId, index, newsArticles));
        }

        public string UploadWeChatNewsMaterial(UploadFileDto uploadFileDto)
        {
            if (string.IsNullOrWhiteSpace(uploadFileDto.FileName))
            {
                throw new ArgumentException("文件名称不能为空。");
            }
            if (uploadFileDto.ContentLength <= 0)
            {
                throw new ArgumentException("文件内容不能为空。");
            }
            if (uploadFileDto.ContentLength > 1024 * 1024)
            {
                throw new ArgumentException("微信图文素材应小于1M。");
            }
            if (string.IsNullOrWhiteSpace(uploadFileDto.FileSuffix))
            {
                throw new ArgumentException("文件后缀不能为空。");
            }
            if (string.IsNullOrWhiteSpace(uploadFileDto.FilePath))
            {
                throw new ArgumentException("文件路径不能为空。");
            }
            if (!File.Exists(uploadFileDto.FilePath))
            {
                throw new ArgumentException("文件不存在。");
            }
            if (!uploadFileDto.FolderName.Equals("image"))
            {
                throw new ArgumentException("文件不是图片文件，无法上传。");
            }
            if (!".jpg,.jpeg,.png".Contains(uploadFileDto.FileSuffix))
            {
                throw new ArgumentException("微信图文消息内图片仅支持JPG/PNG格式，无法上传。");
            }
            MaterialDto materialDto = new MaterialDto()
            {
                FileLength = uploadFileDto.ContentLength,
                FileType = uploadFileDto.FolderName,
                FileSuffix = uploadFileDto.FileSuffix,
                FileName = uploadFileDto.FileName,
                FullFileName = uploadFileDto.FilePath
            };

            string result = WeChatHttpResponseHandle.ProcessMessage(this.newsMaterialService.UploadNewsMaterial(materialDto));
            string url = JsonConvert.DeserializeObject<dynamic>(result).url;
            weChatNewsImageRecordService.Add(new WeChatNewsImageRecordDto()
            {
                UploadFileRecordNo = uploadFileDto.FileName,
                Url = url
            });
            return url;
        }

        public void WeChatNewsMaterialCheckData(UploadFileDto uploadFileDto)
        {
            this.newsMaterialService.CheckContentLength(new MaterialDto()
            {
                FileLength = uploadFileDto.ContentLength,
                FileType = uploadFileDto.FolderName,
                FileSuffix = uploadFileDto.FileSuffix
            });
        }
    }
}
